<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-receipt-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">合同名称：</label>
            <div class="col-sm-8">
                <select id="contractId" name="contractId" class="form-control m-b">
                    <option th:text="全部" th:value="0"></option>
                    <option th:each="contractInfo : ${contractList}" th:text="${contractInfo.name}"
                            th:value="${contractInfo.contractId}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">工程名称：</label>
            <div class="col-sm-8">
                <select id="projectId" name="projectId" class="form-control m-b">
                    <option value="0">暂无</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">收款账户：</label>
            <div class="col-sm-8">
                <input id="collection" name="collection" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">价格：</label>
            <div class="col-sm-8">
                <input id="price" name="price" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea id="remark" name="remark" class="form-control" type="text"></textarea>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/select/select2.js}"></script>
<script type="text/javascript">
    var prefix = ctx + "system/receipt"
    $("#form-receipt-add").validate({
        rules: {
            price: {
                required: true,
                digits: true
            },
        }
    });

    $("#contractId").on("select2:select", function (e) {
        var contractId = e.params.data.id;
        if (contractId == 0) {
            $("#projectId").empty();
            return false;
        }
        if (contractId != null) {
            $.ajax({
                cache: true,
                type: "POST",
                url: ctx + "system/project/listByContractId",
                data: {
                    "contractId": contractId
                },
                async: false,
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    $("#projectId").empty();
                    for (let index in data) {
                        $("#projectId").append("<option value='" + data[index].projectId + "'>" + data[index].name + "</option>");
                    }
                }
            });
        }
    })

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-receipt-add').serialize());
        }
    }
</script>
</body>
</html>
